/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.wirelesssecuritymodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.utils.WmiNetworkAdapters;
import org.testis.wirelesssecuritymodule.core.WirelessConnectionChecker;
import org.testis.wirelesssecuritymodule.core.WirelessConnectionCheckerFactory;
import org.testis.wirelesssecuritymodule.core.WirelessConnectionTaskExecutor;
import org.testis.wirelesssecuritymodule.core.WirelessSecurityModuleException;
import org.testis.wirelesssecuritymodule.core.WirelessSecurityModuleExceptionType;
import org.testis.wirelesssecuritymodule.data.WirelessSecurityModuleDataObject;

/**
 *
 * @author root
 */
public class WirelessSecurityModuleDisconnectWireless extends AtofModuleAction {

    private WmiNetworkAdapters _wmiAdapters;

    @Override
    public boolean operateRequest(AtofModuleService request) {

        _wmiAdapters = new WmiNetworkAdapters();
        WirelessSecurityModuleDataObject dataObj =
                (WirelessSecurityModuleDataObject) request.getDataObj();
        try {

            LogSenderService.putMessageOnQueue(
                    "WirelessSecurityModuleDisconnectWireless",
                    AtofLogLevel.INFO,
                    "Disconnecting from " + dataObj.getParamInterface(),
                    dataObj.getMessageId());

            dataObj.setStopExecution(true);
            String nicGuid = _wmiAdapters.getGuidByName(dataObj.getParamInterface());
            if (nicGuid == null || nicGuid.isEmpty()) {
                throw new WirelessSecurityModuleException(
                        WirelessSecurityModuleExceptionType.WIRELESS_ADAPTER_NOT_FOUND,
                        "WirelessConnectionSupplier:wireless adapter \"" + dataObj.getParamInterface() + "\" not found. "
                        + "Possible reasons: Network card may be disabled or not connected...");
            }

            WirelessConnectionChecker cliWrapper = WirelessConnectionCheckerFactory.getInstance(nicGuid);
            cliWrapper.runCommand("disconnect");
             dataObj.setNicGuid(nicGuid);
            WirelessConnectionTaskExecutor.getInstance().executeTask(dataObj);

            dataObj.setStopExecution(false);
            boolean result = true;

            LogSenderService.putMessageOnQueue(
                    "WirelessSecurityModuleDisconnectWireless",
                    AtofLogLevel.INFO,
                    "Disconnecting from " + dataObj.getParamInterface()
                    + " resulted:"
                    + Boolean.valueOf(result),
                    dataObj.getMessageId());
            return result;

        } catch (WirelessSecurityModuleException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    ex.getMessage() + " Error type:" + ex.getResultType().name());
        } catch (Exception ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    ex.getMessage());
        }
    }
}
